package com.example.eventbus;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.example.eventbus.databinding.SecondLayoutBinding;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class SecondActivity extends AppCompatActivity {

    private SecondLayoutBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.second_layout);
        binding.tz.setOnClickListener((v -> finish()));
        binding.fs.setOnClickListener(v -> {
            EventBus.getDefault().postSticky(new MessageEvent("发送"));
        });
        binding.zc.setOnClickListener(v -> {
            EventBus.getDefault().register(SecondActivity.this);
        });
    }

    @Subscribe(threadMode = ThreadMode.POSTING,sticky = true)
    public void onMoonEvent(MessageEvent messageEvent){
        binding.text.setText(messageEvent.getString());
    }
}
